/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.security.Digests;
import com.thinkgem.jeesite.common.utils.Encodes;
import com.thinkgem.jeesite.modules.bus.dao.EntDao;
import com.thinkgem.jeesite.modules.bus.dao.VerificationCodeDao;
import com.thinkgem.jeesite.modules.bus.entity.Ent;
import com.thinkgem.jeesite.modules.bus.entity.VerificationCode;
import com.thinkgem.jeesite.modules.bus.service.VerificationCodeService;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 企业信息Service
 * @author ZYJ
 * @version 2019-5-30
 */
@Service
@Transactional(readOnly = false)
public class LoginService {
	
	public static final int HASH_INTERATIONS = 1024;
	public static final int SALT_SIZE = 8;
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private EntDao entDao;
	@Autowired
	private VerificationCodeDao verificationCodeDao;
	@Autowired
	private VerificationCodeService verificationCodeService;
	
	/**
	 * 
	* @Title: savePubUser 
	* @author: ZYJ
	* @date: 2019年5月30日 上午11:13:42 
	* @Description: 注册 新增外网用户和企业信息
	* @param:  user
	* @return: User
	* @throws
	 */
	@Transactional(readOnly = false)
	public User savePubUser(User user) {
		if (user.getIsNewRecord()){
			user.setLoginName(user.getMobile());
			user.setPassword(entryptPassword(user.getPassword()));
			user.setUserType("5");
			//根据电话号判断是否注册过,注册过返回空
			User u = userDao.getByLoginName(user);
			if(null != u){
				user.setRemarks("此电话号已注册过");
				return user;
			}
			//根据身份证号判断是否注册过,注册过返回空
			List<User>list = userDao.getByIdCard(user);
			if(list.size() > 0){
				user.setRemarks("此身份证号已注册过");
				return user;
			}
			//根据电话号判断验证码是否有效，有效继续保存用户信息，无效注册失败
			List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(user.getMobile());
			if(verificationCodes.size() == 0){
				user.setRemarks("请获取验证码");
				return user;
			}else{
				VerificationCode verificationCode = verificationCodeService.getVerificationCode(verificationCodes);
				String code = verificationCode.getCode();
				Date deadline = verificationCode.getDeadline();
				Date nowDate = new Date();
				if(!code.equals(user.getVerificationCode())){
					user.setRemarks("验证码错误");
					return user;
				}else if(nowDate.after(deadline)){
					user.setRemarks("验证码过期");
					verificationCode.setUsedStatus(1);
					verificationCode.preUpdate();
					verificationCodeDao.update(verificationCode);
					return user;
				}else{
					user.setRemarks("");
				}
				//新增用户
				user.preInsert();
				userDao.insert(user);
				//新增企业信息，企业id与用户表id相同
				Ent ent = new Ent();
				ent.preInsert();
				ent.setId(user.getId());
				ent.setEntName(user.getEntName());
				entDao.insert(ent);
				//注册成功将验证码状态置为已用
				verificationCode.setUsedStatus(1);
				verificationCode.preUpdate();
				verificationCodeDao.update(verificationCode);
			}
		}else{
			user.preUpdate();
			userDao.update(user);
		}
		return user;
	}
	
	/**
	 * 
	* @Title: updateUser 
	* @author: ZYJ
	* @date: 2019年6月3日 下午6:16:03 
	* @Description: 更换手机号
	* @param: user
	* @return: User
	* @throws
	 */
	@Transactional(readOnly = false)
	public User updateUser(User user){
		//获取用户信息
		User u = UserUtils.get(UserUtils.getPrincipal().getId());
		//根据新电话号判断验证码是否有效，有效继续修改用户信息，无效修改失败
		//获取该电话号未使用的验证码，为了容错，所以获取的是多条记录
		List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(user.getMobile());
		if(verificationCodes.size() == 0){
			user.setRemarks("请获取验证码");
			return user;
		}else{
			VerificationCode verificationCode = verificationCodeService.getVerificationCode(verificationCodes);
			String code = verificationCode.getCode();
			Date deadline = verificationCode.getDeadline();
			Date nowDate = new Date();
			if(!code.equals(user.getVerificationCode())){
				user.setRemarks("验证码错误");
				return user;
			}else if(nowDate.after(deadline)){
				user.setRemarks("验证码过期");
				verificationCode.setUsedStatus(1);
				verificationCode.preUpdate();
				verificationCodeDao.update(verificationCode);
				return user;
			}else{
				user.setRemarks("");
			}
			UserUtils.clearCache();
			u.setLoginName(user.getMobile());
			u.setMobile(user.getMobile());
			u.preUpdate();
			userDao.update(u);
			verificationCode.setUsedStatus(1);
			verificationCode.preUpdate();
			verificationCodeDao.update(verificationCode);
			UserUtils.clearCache();
		}
		return user;
	}
	
	/**
	 * 
	* @Title: confirmCode 
	* @author: ZYJ
	* @date: 2019年6月10日 下午4:33:35 
	* @Description: 确认验证码
	* @param: user
	* @return: Boolean
	* @throws
	 */
	@Transactional(readOnly = false)
	public User confirmCode(User user){
		//根据电话号校验验证码
		List<VerificationCode> verificationCodes = verificationCodeDao.getByMobile(user.getMobile());
		if(verificationCodes.size() == 0){
			user.setRemarks("请获取验证码");
			return user;
		}else{
			VerificationCode verificationCode = verificationCodeService.getVerificationCode(verificationCodes);
			user.setLoginName(user.getMobile());
			String code = verificationCode.getCode();
			Date deadline = verificationCode.getDeadline();
			Date nowDate = new Date();
			if(!code.equals(user.getVerificationCode())){
				user.setRemarks("验证码错误");
			}else if(nowDate.after(deadline)){
				user = userDao.getByLoginName(user);
				verificationCode.setUpdateBy(user);
				user.setRemarks("验证码过期");
				verificationCode.setUsedStatus(1);
				verificationCode.preUpdate();
				verificationCodeDao.update(verificationCode);
			}else{
				user = userDao.getByLoginName(user);
				verificationCode.setUpdateBy(user);
				user.setRemarks("");
				verificationCode.setUsedStatus(1);
				verificationCode.preUpdate();
				verificationCodeDao.update(verificationCode);
			}
		}
		return user;
	}
	
	/**
	 * 
	* @Title: updatePassword 
	* @author: ZYJ
	* @date: 2019年6月10日 下午7:13:24 
	* @Description: 修改密码
	* @param: @param user
	* @return: ResponseResult
	* @throws
	 */
	public User updatePassword(User user){
		//根据电话号获取用户
		User u = userDao.getByLoginName(user);
		u.setPassword(entryptPassword(user.getNewPassword()));
		userDao.updatePasswordById(u);
		UserUtils.clearCache();
		return user;
	}
	
	/**
	 * 生成安全的密码，生成随机的16位salt并经过1024次 sha-1 hash
	 */
	public static String entryptPassword(String plainPassword) {
		String plain = Encodes.unescapeHtml(plainPassword);
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
		return Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword);
	}
	
}
